package com.csu.cphone.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.vo.ResultVo;
import com.csu.cphone.pojo.ProducerPojo;
import com.csu.cphone.service.ProducerService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：ProducerCntroller
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年12月3日 下午9:41:44
 * @company:
 */
@Controller
@RequestMapping("admin/producer")
@Api(value = "admin", description = "管理后台")
public class ProducerManager
{

    @Autowired
    private ProducerService producerService;

    @ResponseBody
    @ApiOperation(value = "添加", notes = "添加数据")
    @RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<ProducerPojo> add(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "联系电话") @RequestParam(value = "number", required = true) String number,
            @ApiParam(value = "角色：0所有，1上门，2门店") @RequestParam(value = "role", required = true) Integer role,
            @ApiParam(value = "业务：0所有，1维修， 2回收") @RequestParam(value = "bussiness", required = true) Integer bussiness,
            @ApiParam(value = "客户名称") @RequestParam(value = "name", required = true) String name,
            @ApiParam(value = "用户密码") @RequestParam(value = "passwd", required = true) String passwd)
    {

        ResultVo<ProducerPojo> resultVo = new ResultVo<ProducerPojo>();
        // 添加
        ProducerPojo result = producerService.add(number, role, bussiness, name, passwd);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "更新", notes = "更新数据,返回1成功")
    @RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> update(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ProducerPojo producer)
    {

        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 添加
        Integer result = producerService.update(producer);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询", notes = "单个")
    @RequestMapping(value = "/queryById", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<ProducerPojo> queryById(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "用户id") @RequestParam(value = "producerId", required = true) Integer producerId)
    {

        ResultVo<ProducerPojo> resultVo = new ResultVo<ProducerPojo>();
        // 添加
        ProducerPojo result = producerService.queryById(producerId);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询总数", notes = "总数")
    @RequestMapping(value = "/count", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> count(HttpServletRequest request, HttpServletResponse response)
    {
        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 添加
        Integer result = producerService.count();
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询列表", notes = "分页")
    @RequestMapping(value = "/queryList", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<ProducerPojo>> queryList(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "页面大小") @RequestParam(value = "pageSize", required = true) Integer pageSize,
            @ApiParam(value = "开始位置") @RequestParam(value = "startIndex", required = true) Integer startIndex,
            @ApiParam(value = "排序") @RequestParam(value = "order", required = false) String order)
    {

        ResultVo<List<ProducerPojo>> resultVo = new ResultVo<List<ProducerPojo>>();
        // 添加
        if (!"DESC".equalsIgnoreCase(order))
        {
            order = "ASC";
        }
        List<ProducerPojo> result = producerService.queryList(pageSize, startIndex, order);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "删除", notes = "删除数据")
    @RequestMapping(value = "/delById", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> deleteById(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "用id") @RequestParam(value = "producerId", required = true) Integer producerId)
    {

        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 添加
        Integer result = producerService.deleteById(producerId);
        resultVo.setData(result);

        return resultVo;
    }
}
